import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TcpEchoClient {
    private Socket socket = null;
    public TcpEchoClient(String serverIP, int port) throws IOException {
        socket = new Socket(serverIP, port);
    }

    public void start(){
        Scanner scanner = new Scanner(System.in);

        try (InputStream inputStream = socket.getInputStream(); OutputStream outputStream = socket.getOutputStream()) {
            PrintWriter printWriter = new PrintWriter(outputStream);
            Scanner scannerFromSocket = new Scanner(inputStream);
            while(true){
                System.out.println("-> ");
                String request = scanner.next();

                printWriter.println(request);
                printWriter.flush();

                String response = scannerFromSocket.next();
                System.out.printf("req: %s; resp: %s\n", request, response);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        TcpEchoClient tcpEchoClient = new TcpEchoClient("127.0.0.1", 8090);
        tcpEchoClient.start();
    }
}
